package com.zou.service;

import com.zou.commons.constants.Constants;
import com.zou.commons.domain.ReturnObject;
import com.zou.commons.utils.UUIDUtils;
import com.zou.mapper.ManagerMapper;
import com.zou.pojo.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("managerService")
public class ManagerService {
    @Autowired(required = false)
    private ManagerMapper managerMapper;

    /**
     * 登录
     *
     * @param map
     * @return
     */
    public Manager login(Map<String, Object> map) {
        return managerMapper.selectManagerByNameAndPwd(map);
    }

    /**
     * 查询满足条件的管理员列表，隐藏部分数据
     *
     * @param manager
     * @return
     */
    public List<Manager> querySomeManagerByManager(Manager manager) {
        List<Manager> managers = managerMapper.selectSomeManagerByManager(manager);
        for (Manager m : managers) {
            m.setManagerPassword("******");
            if (m.getManagerState().equals(Constants.STATE_ENABLED)) {
                m.setManagerState("启用");
            } else if (m.getManagerState().equals(Constants.STATE_DISABLED)) {
                m.setManagerState("禁用");
            }
        }
        return managers;
    }

    /**
     * 添加管理员
     *
     * @param manager
     * @return
     */
    public boolean addManager(Manager manager) {
        manager.setManagerId(UUIDUtils.getUUID());
        // 默认启用
        manager.setManagerState(Constants.STATE_ENABLED);
        try {
            //调用mapper层方法，保存创建的管理员
            if (managerMapper.insertManager(manager) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据接收id数组删除管理员
     *
     * @param ids
     * @return
     */
    public boolean deleteManagerByIds(String[] ids) {
        try {
            //调用mapper层方法，删除相关管理员
            if (managerMapper.deleteManagerByIds(ids) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改前根据id查询管理员
     *
     * @param id
     * @return
     */
    public Manager queryManagerById(String id) {
        Manager manager = new Manager();
        manager.setManagerId(id);
        manager = managerMapper.selectSomeManagerByManager(manager).get(0);
        // 隐藏部分数据
        if (manager.getManagerState().equals(Constants.STATE_ENABLED)) {
            manager.setManagerState("启用");
        } else if (manager.getManagerState().equals(Constants.STATE_DISABLED)) {
            manager.setManagerState("禁用");
        }
        return manager;
    }

    /**
     * 修改管理员信息
     *
     * @param manager
     * @return
     */
    public boolean updateManager(Manager manager) {
        if (manager.getManagerState().equals("启用")) {
            manager.setManagerState(Constants.STATE_ENABLED);
        } else if (manager.getManagerState().equals("禁用")) {
            manager.setManagerState(Constants.STATE_DISABLED);
        }
        try {
            //调用mapper层方法，修改管理员
            if (managerMapper.updateManager(manager) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}